package com.jeequan.jeepay.service.impl;

//package .service.impl;
import cn.hutool.core.util.StrUtil;
import com.jeequan.jeepay.core.entity.PaymentChannel;
import com.jeequan.jeepay.service.PaymentChannelService;
import com.jeequan.jeepay.service.mapper.PaymentChannelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
//import .entity.PaymentChannel;
//import .mapper.PaymentChannelMapper;
//import .service.PaymentChannelService;
/**
 * 代付通道表;(payment_channel)表服务实现类
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Service
public class PaymentChannelServiceImpl implements PaymentChannelService {
    @Autowired
    private PaymentChannelMapper paymentChannelMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public PaymentChannel queryById(Integer id){
        return paymentChannelMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param paymentChannel 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PaymentChannel> paginQuery(PaymentChannel paymentChannel, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PaymentChannel> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(paymentChannel.getCode())){
            queryWrapper.eq(PaymentChannel::getCode, paymentChannel.getCode());
        }
        if(StrUtil.isNotBlank(paymentChannel.getTitle())){
            queryWrapper.eq(PaymentChannel::getTitle, paymentChannel.getTitle());
        }
        if(StrUtil.isNotBlank(paymentChannel.getMerchantNo())){
            queryWrapper.eq(PaymentChannel::getMerchantNo, paymentChannel.getMerchantNo());
        }
        if(StrUtil.isNotBlank(paymentChannel.getSignKey())){
            queryWrapper.eq(PaymentChannel::getSignKey, paymentChannel.getSignKey());
        }
        if(StrUtil.isNotBlank(paymentChannel.getPaymentGateway())){
            queryWrapper.eq(PaymentChannel::getPaymentGateway, paymentChannel.getPaymentGateway());
        }
        if(StrUtil.isNotBlank(paymentChannel.getNotifyUrl())){
            queryWrapper.eq(PaymentChannel::getNotifyUrl, paymentChannel.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(paymentChannel.getPublicKey())){
            queryWrapper.eq(PaymentChannel::getPublicKey, paymentChannel.getPublicKey());
        }
        if(StrUtil.isNotBlank(paymentChannel.getPrivateKey())){
            queryWrapper.eq(PaymentChannel::getPrivateKey, paymentChannel.getPrivateKey());
        }
        if(StrUtil.isNotBlank(paymentChannel.getPlatformPublicKey())){
            queryWrapper.eq(PaymentChannel::getPlatformPublicKey, paymentChannel.getPlatformPublicKey());
        }
        if(StrUtil.isNotBlank(paymentChannel.getQueryGateway())){
            queryWrapper.eq(PaymentChannel::getQueryGateway, paymentChannel.getQueryGateway());
        }
        //2. 执行分页查询
        Page<PaymentChannel> pagin = new Page<>(current , size , true);
        IPage<PaymentChannel> selectResult = paymentChannelMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param paymentChannel 实例对象
     * @return 实例对象
     */
    public PaymentChannel insert(PaymentChannel paymentChannel){
        paymentChannelMapper.insert(paymentChannel);
        return paymentChannel;
    }

    /**
     * 更新数据
     *
     * @param paymentChannel 实例对象
     * @return 实例对象
     */
    public PaymentChannel update(PaymentChannel paymentChannel){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PaymentChannel> chainWrapper = new LambdaUpdateChainWrapper<PaymentChannel>(paymentChannelMapper);
        if(StrUtil.isNotBlank(paymentChannel.getCode())){
            chainWrapper.set(PaymentChannel::getCode, paymentChannel.getCode());
        }
        if(StrUtil.isNotBlank(paymentChannel.getTitle())){
            chainWrapper.set(PaymentChannel::getTitle, paymentChannel.getTitle());
        }
        if(StrUtil.isNotBlank(paymentChannel.getMerchantNo())){
            chainWrapper.set(PaymentChannel::getMerchantNo, paymentChannel.getMerchantNo());
        }
        if(StrUtil.isNotBlank(paymentChannel.getSignKey())){
            chainWrapper.set(PaymentChannel::getSignKey, paymentChannel.getSignKey());
        }
        if(StrUtil.isNotBlank(paymentChannel.getPaymentGateway())){
            chainWrapper.set(PaymentChannel::getPaymentGateway, paymentChannel.getPaymentGateway());
        }
        if(StrUtil.isNotBlank(paymentChannel.getNotifyUrl())){
            chainWrapper.set(PaymentChannel::getNotifyUrl, paymentChannel.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(paymentChannel.getPublicKey())){
            chainWrapper.set(PaymentChannel::getPublicKey, paymentChannel.getPublicKey());
        }
        if(StrUtil.isNotBlank(paymentChannel.getPrivateKey())){
            chainWrapper.set(PaymentChannel::getPrivateKey, paymentChannel.getPrivateKey());
        }
        if(StrUtil.isNotBlank(paymentChannel.getPlatformPublicKey())){
            chainWrapper.set(PaymentChannel::getPlatformPublicKey, paymentChannel.getPlatformPublicKey());
        }
        if(StrUtil.isNotBlank(paymentChannel.getQueryGateway())){
            chainWrapper.set(PaymentChannel::getQueryGateway, paymentChannel.getQueryGateway());
        }
        //2. 设置主键，并更新
        chainWrapper.eq(PaymentChannel::getId, paymentChannel.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(paymentChannel.getId());
        }else{
            return paymentChannel;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = paymentChannelMapper.deleteById(id);
        return total > 0;
    }
}